package diamondRingServlet;

import diamondRingEntity.Admin;
import diamondRingEntity.DiamondRingUser;
import diamondRingEntity.User;
import diamondRingService.impl.AdminServiceImpl;
import diamondRingService.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

/*
用户登录
 */
@WebServlet("/back-Login-Servlet")
public class BackLoginServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置编码格式
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        response.setCharacterEncoding("utf-8");
        PrintWriter out=response.getWriter();

        AdminServiceImpl adminService = new AdminServiceImpl();
        String user=null;
        user=request.getParameter("user");
        String mima="0";
        mima=request.getParameter("mima");
        Admin admin = new Admin(user,mima);
        int count = adminService.Login(admin);
        HttpSession session= request.getSession();
        if(count>0)
        {
            session.setAttribute("user",admin.getName());
            session.setAttribute("mima",admin.getPwd());
        }
        out.print(count);
        out.flush();
        out.close();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
